package com.ocom.payconfig.controller;

import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityXfDeviceInfo;
import com.ocom.common.entity.card.EntityXfPointInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.AddTollPointInfoRequest;
import com.ocom.common.request.payconfig.GetTollPointInfoRequest;
import com.ocom.common.request.payconfig.UpdateTollPointInfoRequest;
import com.ocom.common.request.web.DelTollPointRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.service.CcbBusinessInfoService;
import com.ocom.payconfig.service.DishesCommonService;
import com.ocom.payconfig.service.XfDeviceInfoService;
import com.ocom.payconfig.service.XfPointInfoService;
import com.ocom.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 收费点信息
 消费2.11	getTollPointInfo
 消费2.12	addTollPointInfo
 消费2.13	updateTollPointInfo
 */
@Slf4j
@RestController
public class TollPointInfoController {

    @Autowired
    XfPointInfoService xfPointInfoService;

    @Autowired
    XfDeviceInfoService xfDeviceInfoService;

    @Autowired
    DishesCommonService dishesCommonService;

    @Resource
    private CcbBusinessInfoService ccbBusinessInfoService;

    @Resource
    RedisUtil redisUtil;

    // 消费2.11 .获取收费点信息(getTollPointInfo)
    @PostMapping(PayConfigUrl.Url_getTollPointInfo)
    public Result getTollPointInfo(@RequestBody GetTollPointInfoRequest request, HttpServletRequest httpServletRequest){
//        System.out.println(request);
//        log.info(httpServletRequest.getHeader("Authorization"));
//        log.info(SecurityUtils.getYoCiUser().toString());
        return ResultUtil.success(xfPointInfoService.getTollPointInfo(request.getCom_id(),request));
    }

    // 2.12.添加收费点信息(addTollPointInfo)
    @PostMapping(PayConfigUrl.Url_addTollPointInfo)
    public Result addTollPointInfo(@RequestBody AddTollPointInfoRequest request){
        EntityXfPointInfo entityXfPointInfo=xfPointInfoService.addTollPointInfo(request.getComId(),request);
        if(entityXfPointInfo.getPointId()>0){
            return ResultUtil.success(entityXfPointInfo);
        }else{
            return ResultUtil.error(ResultEnum.Err_2016);
        }
    }

    // 2.13.修改收费点信息(addTollPointInfo)
    @PostMapping(PayConfigUrl.Url_updateTollPointInfo)
    public Result updateTollPointInfo(@RequestBody UpdateTollPointInfoRequest request){
        EntityXfPointInfo entityXfPointInfo0 = xfPointInfoService.getTollPointInfo(request.getCom_id(),request.getPoint_id());

        EntityXfPointInfo entityXfPointInfo=xfPointInfoService.updateTollPointInfo(request.getCom_id(),request);

        List<EntityXfDeviceInfo> xfDeviceInfos =  xfDeviceInfoService.getMachList(request.getCom_id(),request.getPoint_id());

        if(xfDeviceInfos!= null && xfDeviceInfos.size()>0){
            for (EntityXfDeviceInfo entityXfDeviceInfo : xfDeviceInfos){
                String key ="xf_device_info#comId:"+entityXfDeviceInfo.getComId()+"#machNo:"+entityXfDeviceInfo.getDeviceNo()+"#";
                redisUtil.del(key);
            }
        }
        try {
            if (!entityXfPointInfo0.getMerchId().equals(request.getMerch_id())) {
                dishesCommonService.resetPointDishesWhite(request.getCom_id(), request.getPoint_id());
                ccbBusinessInfoService.updateDeviceByCcb(request.getCom_id(), request);//（ver 1.2.45）2.15修改收费点信息(updateTollPointInfo) 新增逻辑
            }
        } catch (Exception e) {
            log.info("更新消费点异常 ，【{}】【{}】", request, e);
        }

        return ResultUtil.success(entityXfPointInfo);
    }


    // 2.13.修改收费点信息(addTollPointInfo)
    @PostMapping(PayConfigUrl.Url_deleteTollPointInfo)
    public Result deleteTollPointInfo(@RequestBody DelTollPointRequest request){
        xfPointInfoService.deleteTollPointInfo(request.getComId(),request);
        return ResultUtil.success();
    }



    @PostMapping("/901222")
    public Result getTollPointInfo2(){
        return ResultUtil.success(xfPointInfoService.getTollPointList(1l));
    }


}
